package com.hu.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hu.auth.common.utils.MD5;
import com.hu.auth.common.utils.MenuUtil;
import com.hu.auth.mapper.UserMapper;
import com.hu.auth.model.Menu;
import com.hu.auth.model.User;
import com.hu.auth.model.po.UserAddPo;
import com.hu.auth.model.po.UserPo;
import com.hu.auth.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    UserMapper userMapper;

    @Override
    public Page<User> list(int page, int limit, UserPo po) {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (po.getCreateTimeBegin() != null) {
            lambdaQueryWrapper.ge(User::getCreateTime, po.getCreateTimeBegin())
                    .le(User::getCreateTime, po.getCreateTimeEnd());
        }
        if (po.getKeyword() != null) {
            lambdaQueryWrapper.and(wrapper -> {
                wrapper.like(User::getName, po.getKeyword())
                        .or().like(User::getPhone, po.getKeyword())
                        .or().like(User::getUsername, po.getKeyword());
            });
        }
        return userMapper.selectPage(new Page<>(page, limit), lambdaQueryWrapper);
    }

    @Override
    public List<String> getUserPerms(Long id) {
        return userMapper.getMenuByRoleIds(userMapper.getRoleIdByUserId(id))
                .stream()
                .filter(menu -> menu.getType() == 2)
                .map(Menu::getPerms)
                .collect(Collectors.toList());
    }

    @Override
    public void add(UserAddPo po) {
        User user = new User();
        user.setUsername(po.getUsername());
        user.setPassword(MD5.encrypt(po.getPassword()));
        user.setName(po.getName());
        user.setPhone(po.getPhone());
        user.setStatus((byte) 1);
        userMapper.insert(user);
    }

    @Override
    public Map<String, Object> info(Long id) {
        User user = userMapper.selectById(id);
        List<String> roleNameList = userMapper.getRoleNameByUserId(id);
        List<Menu> menuList = userMapper.getMenuByRoleIds(userMapper.getRoleIdByUserId(id));

        Map<String, Object> map = new HashMap<>();
        map.put("name", user.getName());
        map.put("avatar", user.getHeadUrl());
        map.put("roles", roleNameList);
        // map.put("routers", MenuUtil.buildRouters(MenuUtil.sort(menuList, 0L)));
        map.put("routers", MenuUtil.sort1(menuList.stream().filter(menu -> menu.getComponent() != null).collect(Collectors.toList()), 0L));
        map.put("buttons", getUserPerms(id));
        return map;
    }
}
